package com.by.yz.loginUtil;

import android.content.Context;
import android.util.Log;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.by.yz.App;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;


public class AccountRESTFul {

    public interface IhttpCb{
        public void ok(JSONObject response);
        public void error(String error);
    }

    public static String api = "/api/account";

    public AccountRESTFul() {

    }
    public void get(final IhttpCb cb) {
        String url = Constants.server_url + api;

        JsonObjectRequest getCurrentUser = new JsonObjectRequest(Request.Method.GET, url, null,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        cb.ok(response);
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        error.printStackTrace();
                        if (cb!=null) {
                            cb.error(error.toString());
                        }
                    }
                }
        ){


            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                HashMap<String, String> headers = new HashMap<String, String>();
                headers.put("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
                return headers;
            }
        };
        App.requestQueue.add(getCurrentUser);
    }
}

